package edu.nf.supermarket.web.advice;

import edu.nf.supermarket.exception.GlobalException;
import edu.nf.supermarket.result.ResultVO;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @author yjj
 * @date 2025-05-19
 */
@RestControllerAdvice(basePackages = "edu.nf.supermarket.web.controller")
public class GlobalExceptionAdvice {
    /**
     * 处理最顶层的自定义异常
     * @param e
     * @return
     */
    @ExceptionHandler(GlobalException.class)
    public <T> ResultVO<T> handleGlobalException(GlobalException e){
        ResultVO<T> resultVO = new ResultVO();
        resultVO.setCode(e.getErrorCode());
        resultVO.setMessage(e.getMessage());
        return resultVO;
    }

    /**
     * 捕获一个最大的异常兜底
     * @param e
     * @return
     * @param <T>
     */

    @ExceptionHandler(Exception.class)
    public <T> ResultVO<T> handlelException(Exception e){
        if(e==null){
            Thread.dumpStack();
        }
        e.printStackTrace();
        ResultVO<T> resultVO = new ResultVO();
        resultVO.setCode(HttpStatus.INTERNAL_SERVER_ERROR.value());
        resultVO.setMessage("服务器繁忙, 请稍后..."+e.getMessage());
        return resultVO;
    }
}
